package com.monitor.module.sediment.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monitor.entity.pojo.camera.Camera;
import com.monitor.entity.pojo.sediment.Sediment;
import com.monitor.entity.request.sediment.SedimentFindRequest;
import com.monitor.entity.response.ResultCode;
import com.monitor.entity.response.Rs;
import com.monitor.module.camera.service.CameraService;
import com.monitor.module.sediment.mapper.SedimentMapper;
import com.monitor.module.sediment.service.SedimentService;
import com.monitor.utils.Base64Utils;
import com.monitor.utils.PicUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * .
 *
 * @author Li Zhiyuan
 * @since 2020-11-18
 */
@Service
@Slf4j
public class SedimentServiceImpl extends ServiceImpl<SedimentMapper, Sediment> implements SedimentService {
    @Resource
    private SedimentMapper mapper;
    @Resource
    private CameraService cameraService;

    @Override
    public ResponseEntity<Rs<Object>> upload(String mac, String licensePlate, MultipartFile frontPhoto, MultipartFile bodyPhoto) {
        Sediment sediment = new Sediment();
        try {
            String image = "data:image/jpg;base64,";
            sediment.setFrontPhoto(image + Base64Utils.encode(PicUtils.compressPicForScale(frontPhoto.getBytes(), 200)));
            sediment.setBodyPhoto(image + Base64Utils.encode(PicUtils.compressPicForScale(bodyPhoto.getBytes(), 200)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //根据mac查询摄像头数据
        Camera camera = cameraService.getOne(Wrappers.<Camera>lambdaQuery().eq(Camera::getMac, mac));
        if (camera == null)
            return Rs.fail(ResultCode.MAC);
        sediment.setMac(mac);
        sediment.setCompanyId(camera.getCompanyId());
        sediment.setLicensePlate(licensePlate);
        sediment.setMouth(camera.getMouth());
        sediment.setEntryName(camera.getEntryName());
        save(sediment);
        return Rs.ok(ResultCode.UPLOAD);
    }

    @Override
    @Cacheable(value = "Sediment",key = "#request")
    public Page<Sediment> findPage(SedimentFindRequest request) {

        //页码
        Integer current = request.getCurrent();
        //条数
        Integer size = request.getSize();

        log.info("session==========={}", (String) SecurityUtils.getSubject().getSession().getId());
        //当前账号所属企业
//        Long companyId=UserUtil.getSession().getCompanyId();
        Long companyId = request.getCompanyId();


        return page(new Page<>(current, size), Wrappers.<Sediment>lambdaQuery()
                .like(request.getLicensePlate() != null && request.getLicensePlate() != "", Sediment::getLicensePlate, request.getLicensePlate())
                .eq(request.getEntryName() != null && request.getEntryName() != "", Sediment::getEntryName, request.getEntryName())
                .eq(Sediment::getCompanyId, companyId)
                .ge(request.getStartTime() != null, Sediment::getCreateTime, request.getStartTime())
                .le(request.getEndTime() != null, Sediment::getCreateTime, request.getEndTime())
                //倒叙排序
                .orderByDesc(Sediment::getCreateTime));

    }
}
